/*
 * Indexer.java
 *
 * Created on 6 March 2006, 13:05
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package mac5855.app.lucene.search;

import java.io.IOException;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Document;
import mac5855.app.dao.DAOFactory;
import mac5855.app.models.Pergunta;
import mac5855.app.models.Resposta;

import org.apache.lucene.analysis.br.BrazilianAnalyzer;
import org.apache.lucene.analysis.br.BrazilianStemFilter;
import org.apache.lucene.analysis.br.BrazilianStemmer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.SimpleFSLockFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author John
 */
public class Indexer {
    
    /** Creates a new instance of Indexer */
    public Indexer() {
    }
 
    private IndexWriter indexWriter = null;
    
    public IndexWriter getIndexWriter(boolean create) throws IOException {
        if (indexWriter == null) {
            indexWriter = new IndexWriter(FSDirectory.getDirectory("index-directory"),
                                          new BrazilianAnalyzer(), true);
        }
        return indexWriter;
   }    
   
    public void closeIndexWriter() throws IOException {
        if (indexWriter != null) {
            indexWriter.close();
        }
   }
    
    public void indexPergunta(Pergunta pergunta) throws IOException {

        System.out.println("Indexing pergunta: " + pergunta);
        IndexWriter writer = getIndexWriter(false);
        Document doc = new Document();
        doc.add(new Field("id", pergunta.getId().toString(), Field.Store.YES, Field.Index.NO));
        doc.add(new Field("titulo", pergunta.getTitulo(), Field.Store.YES, Field.Index.TOKENIZED));
        doc.add(new Field("categoria", pergunta.getCategoria().getNome(), Field.Store.YES, Field.Index.UN_TOKENIZED));
        
        StringBuffer fullSearchableText = new StringBuffer();
        fullSearchableText.append(Analisador.remover(pergunta.getTitulo())+" ");
        fullSearchableText.append(Analisador.remover(pergunta.getCategoria().getNome())+" ");
        for(Resposta resposta : pergunta.getRespostas())
        	fullSearchableText.append(Analisador.remover(resposta.getTitulo())+" ");
                
        doc.add(new Field("content", fullSearchableText.toString(), Field.Store.NO, Field.Index.TOKENIZED));
        writer.addDocument(doc);
    }   
    
    public void rebuildIndexes() throws IOException {
          //
          // Erase existing index
          //
          getIndexWriter(true);
          //
          // Index all Accommodation entries
          
          ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                  DAOFactory.path );
          DAOFactory daoFactory = ( (DAOFactory) context.getBean( "daoFactory" ) );
          
          
          Pergunta[] perguntas = daoFactory.getPerguntaDAO().findAll();
          //
          
          for(Pergunta pergunta : perguntas) {
              indexPergunta(pergunta);              
          }
          //
          // Don't forget to close the index writer when done
          //
          closeIndexWriter();
     }    
    
  
    
    
}
